package com.cslc.mp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.NamingException;

import com.cslc.mp.dao.DatabaseConnection;
import com.cslc.mp.dao.DAO.IUserDAO;
import com.cslc.mp.dao.vo.User;

public class UserDAOImpl implements IUserDAO {
	
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	public UserDAOImpl(Connection conn) {
		this.conn = conn;
	}
	@Override
	public boolean queryUser(User user) {
		int count = 0;
		boolean flag = false;
		String sql = "select count(*) from user where terminal_no = ? and saler_no = ? and password = ?";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, user.getTerminalNo());
			pstmt.setString(2, user.getSalerNo());
			pstmt.setString(3, user.getPassword());
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				count = rs.getInt(1);
			}
			if(count > 0) {
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public User findById(String openid) {
		User user = null;
		String sql = "select * from user where openid=?";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, openid);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				user = new User();
				user.setOpenid(rs.getString(2));
				user.setTerminalNo(rs.getString(3));
				user.setSalerNo(rs.getString(4));
				user.setPassword(rs.getString(5));
			}
			pstmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}
	@Override
	public boolean bindUser(User user) {
		boolean flag = false;
		String sql = "update user set openid = ? where terminal_no = ? and saler_no = ? and password = ?";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, user.getOpenid());
			pstmt.setString(2, user.getTerminalNo());
			pstmt.setString(3, user.getSalerNo());
			pstmt.setString(4, user.getPassword());
			
			if(pstmt.executeUpdate() > 0 ) {
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	@Override
	public boolean unbendUser(String openid) {
		boolean flag = false;
		String sql = "update user set openid = \"\" where openid = ?";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, openid);
			if(pstmt.executeUpdate() > 0 ) {
				flag = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return flag;
	}
	@Override
	public boolean bindUser(String openid, String terminalNo) {
		boolean flag = false;
		String sql = "update user set openid = ? where terminal_no = ?";
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, openid);
			pstmt.setString(2, terminalNo);
			if(pstmt.executeUpdate() > 0 ) {
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
}
